﻿using System.Xml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication
{
    public abstract class FeedBase : IEnumerable, IFeed
    {
        internal ArrayList BaseItems { get { return this._items; } }
        
        public string Title { get { return GetProperty("title", string.Empty) as string; } set { SetProperty("title", value); } }

        public int Count { get { return this._items.Count; } }
        public virtual FeedType FeedType { get { return FeedType.Unknown; } }
        public virtual string ContentType { get { return string.Empty; } }

        internal Dictionary<string, object> FeedProperties { get { return this._feedproperties; } }

        private Dictionary<string, object> _feedproperties = null;
        private ArrayList _items = null;

        public FeedBase()
        {
            this._feedproperties = new Dictionary<string, object>();
            this._items = new ArrayList();
            Clear();
        }

        public virtual int Add(IFeedItem item)
        {
            return BaseItems.Add(item);
        }

        protected void Clear()
        {
            this._feedproperties.Clear();
            this._items.Clear();
            this._feedproperties.Add("generator", "Marbles Web Platform");
        }

        protected void SetProperty(string key, object value)
        {
            if (this._feedproperties.ContainsKey(key))
            {
                this._feedproperties[key] = value;
            }
            else
            {
                this._feedproperties.Add(key, value);
            }
        }

        protected object GetProperty(string key, object defaultvalue)
        {
            if (this._feedproperties.ContainsKey(key))
            {
                return this._feedproperties[key];
            }

            return defaultvalue;
        }

        protected object GetProperty(string key)
        {
            if (this._feedproperties.ContainsKey(key))
            {
                return this._feedproperties[key];
            }

            return null;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return BaseItems.GetEnumerator();
        }

        public virtual XmlDocument ToXml()
        {
            return null;
        }

    }
}
